<template>
  <BeTable
    class="offline-download-list"
    rowKey="id"
    :columns="columns"
    :pagination="{ pageSize: 15, total: 100 }"
    :data-source="tableData"
    :formSetting="{ fields: searchFields }"
    :beforeFetch="beforeFetch"
    :api="getDownloadList"
  >
    <!-- 自定义单元格内容 -->
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <div class="options-btns">
          <BeButton type="link" @click="handleDownload(record)">下载</BeButton>
        </div>
      </template>
    </template>
  </BeTable>
</template>

<script lang="ts" setup>
// import { AssetsModal } from '@/components/business/modal/AssetsModal'
import { dmsMaskCenterApi } from '@/api/dms-mask-center'
import { GetDownloadList } from '@/api/dms-mask-center/type'
import { downloadFileByBlob } from '@/utils'
import { message, TableColumnsType } from 'ant-design-vue'
import dayjs from 'dayjs'

defineOptions({
  name: 'OfflineDownloadService'
})

// state
const selectedRowKeys = ref([]) // 选择的数据
// 表单字段定义（与 SearchForm 中的 prop 类型一致）
const searchFields = [
  {
    label: '文件名',
    prop: 'fileName',
    component: 'Input',
    componentProps: { placeholder: '请输入文件名' }
  },
  {
    label: '工单号',
    prop: 'ticketNumber',
    component: 'Input',
    componentProps: { placeholder: '请输入工单号' }
  },
  {
    label: '授权人',
    prop: 'createUser',
    component: 'Input',
    componentProps: { placeholder: '请输入授权人' }
  },
  {
    label: '上传时间',
    prop: 'startTimeRange',
    component: 'RangePicker',
    componentProps: {
      showTime: false,
      format: 'YYYY-MM-DD'
    }
  }
]
// 表格数据
const tableData = ref()
// 表格定义
const columns: TableColumnsType = [
  { title: '任务名称', align: 'left', fixed: 'left', dataIndex: 'name', key: 'name', width: '14%' },
  { title: '文件名', align: 'left', fixed: 'left', dataIndex: 'fileName', key: 'fileName', width: '14%' },
  { title: '资源产', align: 'left', dataIndex: 'sourceResourceName', key: 'sourceResourceName' ,width: '8%'},
  { title: '工单编号', align: 'left', dataIndex: 'ticketNumber', key: 'ticketNumber', width: '16%'},
  { title: '授权人', align: 'left', dataIndex: 'allowDownloadUserNames', key: 'allowDownloadUserNames',width: '10%' },
  { title: '供给量（MB）', align: 'left', dataIndex: 'transferDiskSpace', key: 'transferDiskSpace' ,width: '12%'},
  { title: '供给量（行）', align: 'left', dataIndex: 'transferRowCount', key: 'transferRowCount' ,width: '12%'},
  { title: '上传时间', align: 'left', dataIndex: 'createTime', key: 'createTime' },
  { title: '操作', align: 'center', key: 'action', width: '4%' }
]
// CheckBox选择配置
const _rowSelection = ref({
  selectedRowKeys: selectedRowKeys,
  onChange: (keys: (string | number)[], _selectedRows: any) => {
    selectedRowKeys.value = keys as any
  }
})

// 搜索前参数处理
const beforeFetch = (params: any) => {
  if (params.startTimeRange) {
    if (params.startTimeRange[0]) params.createBeginTime = dayjs(params.startTimeRange[0]).format('YYYY-MM-DD HH:mm:ss')
    if (params.startTimeRange[1]) params.createEndTime = dayjs(params.startTimeRange[0]).format('YYYY-MM-DD HH:mm:ss')
  }
  delete params.startTimeRange
  return params
}

// 获取下载记录列表
const getDownloadList = async (params?: GetDownloadList) => {
  return dmsMaskCenterApi.getDownloadList({ data: { ...params } })
}

const handleDownload = async (record: any) => {
  // alert(window.location.host);
  // return;
  // window.open('https://' + window.location.host + '/dms-mask-center/api/v1/dataoffline/task/downloadRecord/watermark-file?id=' + record.id + '&taskId=' + record.taskId)
  try {
    const response = await dmsMaskCenterApi.getDownload({ data: { id: record.id, taskId: record.taskId } }, { globAlToast: false })
    // 使用你封装的方法下载文件
	console.log(response.code);
	if(!response.code){
		window.open('https://' + window.location.host + '/dms-mask-center/api/v1/dataoffline/task/downloadRecord/watermark-file?id=' + record.id + '&taskId=' + record.taskId)
	}else{
		message.error(response.message);
	}
	// window.open('https://' + window.location.host + '/dms-mask-center/api/v1/dataoffline/task/downloadRecord/watermark-file?id=' + record.id + '&taskId=' + record.taskId)
    // downloadFileByBlob(response.data, record.id)
  } catch (error: any) {
    
  }
}
</script>
<style lang="scss" scoped>
.offline-download-list {
  // padding: 0 20px;
  background-color: var(--color-white);
  .options-btns {
    display: flex;
    justify-content: center;
  }
}
</style>
